package utill;

import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.commons.dbcp.BasicDataSource;

public class DBUtil {
	private static BasicDataSource dSource;
	private static final String classname="oracle.jdbc.OracleDriver";
	private static final String url="jdbc:oracle:thin:@ZHA-ITA077-w7:1521:ita3";
	private static final String usernameString="angers";
	private static final String password="123";
	static
	{
		dSource=new BasicDataSource();
		dSource.setUrl(url);
		dSource.setDriverClassName(classname);
		dSource.setUsername(usernameString);
		dSource.setPassword(password);
	}

	public static Connection createConnection(){
		String classname="oracle.jdbc.OracleDriver";
		String url="jdbc:oracle:thin:@ZHA-ITA077-w7:1521:ita3";
		String usernameString="angers";
		String password="123";
		Connection connection=null;
		try {
			Class.forName(classname);
			connection= DriverManager.getConnection(url,usernameString,password);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
//		System.out.println(connection);
		return connection;
	}
	
	public static Connection createConnectionWithDataSouce() throws SQLException, ClassNotFoundException
	{
		Connection conn1 = dSource.getConnection();
		return conn1;
		
	}
	
	public static void close(Connection connection,PreparedStatement pst,ResultSet rs){
		try {
			connection.close();
			pst.close();
			if(rs!=null)
			   rs.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
}
